package com.example.zyfypt_525lmj.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.example.zyfypt_525lmj.R;
import com.example.zyfypt_525lmj.bean.UserBean;
import com.example.zyfypt_525lmj.iface.loginlistener;
import com.example.zyfypt_525lmj.model.loginModer;


public class login_activity extends AppCompatActivity implements loginlistener ,View.OnClickListener {
    private EditText et_userName, et_password;
    private Button btn_login, btn_register;
    private CheckBox checkBox_remember;
    private SharedPreferences preferences;
    final static String PREF_NAME="loginPref";
    private Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_activity);
        context=login_activity.this;

        init();
        preferences= getSharedPreferences(PREF_NAME,MODE_PRIVATE);
        boolean isChecked =preferences.getBoolean("isChecked",false);
        if(isChecked)
        {
            String userName=preferences.getString("userName","");
            String password =preferences.getString("password","");
            et_userName.setText(userName);
            et_password.setText(password);
            checkBox_remember.setChecked(isChecked);
        }

    }

    public void init() {
        et_password = (EditText) findViewById(R.id.mima);
        et_userName = (EditText) findViewById(R.id.huyongming);
        btn_login = (Button) findViewById(R.id.denglu);
        btn_register = (Button) findViewById(R.id.zhuce);
        btn_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);
        checkBox_remember = (CheckBox) findViewById(R.id.jizhuzhanghao);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.denglu:
                String s_username = et_userName.getText().toString();
                String password = et_password.getText().toString();
                loginModer mode = new loginModer();
                mode.login(s_username, password, this);
                break;
            case R.id.zhuce:
                Intent intent=new Intent(context,register_activity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
        public void onResponse(UserBean userBean){
            String sessionId=userBean.getSessionid();
            SharedPreferences.Editor editor =preferences.edit();
            editor.putString("sessionId",sessionId);
            editor.putBoolean("isChecked",checkBox_remember.isChecked());
            if(checkBox_remember.isChecked()){
                editor.putString("userName",et_userName.getText().toString());
                editor.putString("password",et_password.getText().toString());

            }
            editor.commit();
            Intent intent =new Intent(context,MainActivity.class);
            startActivity(intent);
            finish();

        }


        public void onFail(String msg){

        }


    }
